from os import listdir, path, rename


def replace_in_filenames(folder_path: str, old_prefix: str, new_prefix: str):
    """
    批量重命名文件
    :param folder_path: 文件夹路径
    :param old_prefix: 需要替换的旧前缀
    :param new_prefix: 新前缀
    """
    files = listdir(folder_path)
    for filename in files:
        file_path = path.join(folder_path, filename)

        # 只处理文件（排除目录）
        if not path.isfile(file_path):
            continue

        if filename.startswith(old_prefix):
            new_name: str = f"{new_prefix}{filename[len(old_prefix):]}"

            # 如果新文件名已存在，添加序列号
            counter = 1
            while path.exists(path.join(folder_path, new_name)):
                new_name = f"{new_prefix}{filename[len(old_prefix):]}_{counter}"
                counter += 1

            rename(file_path, path.join(folder_path, new_name))

            print(f"已重命名：{filename} → {new_name}")


def main():
    replace_in_filenames(r"/mnt/e/权利的游戏", "[www.domp4.cc]权l的y戏", "权利的游戏")


if __name__ == "__main__":
    main()
